<?php

class Staffusers extends CI_Model{
    function __construct(){
        parent::__construct();
    }
    
    function login($login,$pass){
        $this->db->where("staffuser_login",$login);
        $this->db->where("staffuser_password",$pass);
        $query = $this->db->get("staffusers");
        if ($query->num_rows() == 0){
            return null;
        }
        else{
            return $query->row();
        }
    }

    function insertStaffuser($current_role, $staffuser_id, $staffuser_login, $staffuser_email, $staffuser_title, $staffuser_role, $staffuser_password = null) {
        
        if ($staffuser_role > $current_role){
            return false;
        }
        
        $data['staffuser_login'] = $staffuser_login;
        $data['staffuser_email'] = $staffuser_email;
        $data['staffuser_title'] = $staffuser_title;
        $data['staffuser_role'] = $staffuser_role;

        $staffuser_id = intval($staffuser_id);

        if ($staffuser_password != null) {
            $data['staffuser_password'] = $staffuser_password;
        }

        $this->db->trans_start();

        if ($staffuser_id > 0) {
            $this->db->where('staffuser_id', $staffuser_id);
            $this->db->update('staffusers', $data);
        } else {
            $query = $this->db->insert('staffusers', $data);
            $staffuser_id = $this->db->insert_id();
        }

        $this->db->trans_complete();

        return $this->db->trans_status();
    }

    function getByID($staffuser_id) {
        $this->db->from('staffusers');
        $this->db->where('staffuser_id', $staffuser_id);
        $staffuser = $this->db->get();
        if ($staffuser->num_rows() > 0) {
            return $staffuser->row();
        } else {
            return null;
        }
    }

    function getByLogin($staffuser_login) {
        $this->db->from('staffusers');
        $this->db->where('staffuser_login', $staffuser_login);
        
        $staffuser = $this->db->get();
        if ($staffuser->num_rows() > 0) {
            return $staffuser->row();
        } else {
            return null;
        }
    }

    function getByEmail($staffuser_email) {
        $this->db->from('staffusers AS U');
        $this->db->where('staffuser_email', $staffuser_email);
        $staffuser = $this->db->get();
        if ($staffuser->num_rows() > 0) {
            return $staffuser->row();
        } else {
            return null;
        }
    }

    function getLikeName($term, $role) {
        if (trim($term) != "") {
            $this->db->like("staffuser_title", $term);
        }
        
        if ($role != admin_role()){
            $this->db->where('staffuser_role <=', $role);
        }
        $this->db->order_by('staffuser_title', "asc");
        return $this->db->get("staffusers");
    }

    function getLikeEmail($term, $role) {
        if (trim($term) != "") {
            $this->db->like("staffuser_email", $term);
        }
        
        if ($role != 3){
            $this->db->where('staffuser_role <', $role);
        }

        $this->db->order_by('staffuser_email', "asc");
        return $this->db->get("staffusers");
    }

    function getLikeUsername($term, $role) {
        if (trim($term) != "") {
            $this->db->like("staffuser_login", $term);
        }
        
        if ($role != 3){
            $this->db->where('staffuser_role <', $role);
        }
        $this->db->order_by('staffuser_login', "asc");
        return $this->db->get("staffusers");
    }


    function deleteStaffuser($staffuser_id, $role) {

        $this->db->trans_start();
        
        if ($role != 3){
            $this->db->where('staffuser_role <', $role);
        }
        //You can't delete yourself!
        $this->db->where("staffuser_id !=", $this->session->userdata('user_id'));

        $this->db->where("staffuser_id", $staffuser_id);
        $this->db->delete("staffusers");

        if ($this->db->affected_rows() == 0){
            return false;
        }
        
        $this->db->trans_complete();

        
        return $this->db->trans_status();
    }
    
    function updatePassword($staffuser_id, $old_password, $new_password){
        $this->db->where('staffuser_id', $staffuser_id);
        $this->db->where('staffuser_password', $old_password);
        $query = $this->db->get('staffusers');
        
        if ($query->num_rows == 0){
            return false;
        }
        
        $this->db->trans_start();
        
        $this->db->where('staffuser_id', $staffuser_id);
        $this->db->set('staffuser_password', $new_password);
        $this->db->update('staffusers');
        
        $this->db->trans_complete();
        
        return $this->db->trans_status();
    }
}

